<?php
// +----------------------------------------------------------------------
// | 产品分销价格配置，兼容多级转分销
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------

if(isset($_REQUEST['action']) && $_REQUEST['action']=='list'){// 异步获取数据
	
	include_once("/var/www/html/new/com.inc.php");
	include_once("/var/www/html/new/d/class/abc/Product.class.php");
	include_once("/var/www/html/new/d/class/abc/Member.class.php");
	
	// pid=2884&sid=81&lvl=1&aids=81
	$ac = $GLOBALS['_ac'];
	$pw = $GLOBALS['_pw'];
	$inSide  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
	
	if($_REQUEST['type']==1){


		$sid = $_SESSION['sid'];// 登陆账号ID
		$aid = isset($_REQUEST['aid']) ? $_REQUEST['aid']+0:0;// 产品上级供应商ID
		$pid = isset($_REQUEST['pid']) ? $_REQUEST['pid']+0:0;// 产品ID
		
		// if($pid==0 || $aid==0) exit('{"status":"fail","msg":"参数错误"}');
		$today = date('Y-m-d');


		// 1.获取分销商
		$sql = "select r.id,r.son_id,m.dname,m.account from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.parent_id=$sid and r.ship_type=0 and r.status=0 and son_id>1 and son_id<>$sid limit 2";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$distributors[$row['son_id']] = array(
				'dname'   => $row['dname'],
				'account' => $row['account'],
			);
		}

		// 2.各个产品信息
		// $pids = array(10980,15642);
		// $pids = array(11555,11556);
		$pids = array(16506,16507,16508,16509,16510);
		// $aid = 6610;
		$aid = 0;
		$ltitle = '';
		$sql = "select t.title as ttitle,l.title as ltitle,p.id as pid from uu_products p left join uu_jq_ticket t on p.id=t.pid left join uu_land l on p.contact_id=l.id  where p.id in (".implode(',', $pids).") limit ".count($pids);
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$pInfos[$row['pid']] = $row;
			$ltitle = $row['ltitle'];
		}

		$prods = array();
		foreach($pInfos as $pid=>$pInfo){
			
			$dprice = $price = array();
			
			// 会员购买价格
			$price = abc777\Product::Price($inSide, $pid, $aid, $today, 0);
			$jsprice = $price['js'];
			
			// 3.获取分销商已配置的差价
			$dids_arr = array_keys($distributors);
			$dids_str = implode(',', $dids_arr);
			$dids_siz = count($dids_arr);
			$p_aid = $aid ? $sid:0;// 一级分销商
			$sql = "select dprice,pid from uu_priceset where tid=$pid and aid=$p_aid and pid in ($dids_str) order by id desc limit $dids_siz";
			$GLOBALS['le']->query($sql);
			while($row = $GLOBALS['le']->fetch_assoc()){
				$dprice[$row['pid']] = $row['dprice']/100;
			}
			
			// 4.整合数据，各分销商的分销价
			$prod = array('ttitle'=>$pInfo['ttitle'],'jsprice'=>$jsprice,'list'=>array());
			foreach($distributors as $did=>$row){
				$row['js'] = isset($dprice[$did]) ? $jsprice+$dprice[$did]:'';
				$prod['list'][] = $row;
			}
			$prods[$pid] = $prod;
		}



		$response = array(
			'ltitle' => $ltitle,
			'dtotal' => count($distributors),
			'ptotal' => count($pids),
			'prods'  => $prods,
		);
		echo json_encode($response);
		exit;
	}
	


	$sid = $_SESSION['sid'];// 登陆账号ID
	$aid = isset($_REQUEST['aid']) ? $_REQUEST['aid']+0:0;// 产品上级供应商ID
	$pid = isset($_REQUEST['pid']) ? $_REQUEST['pid']+0:0;// 产品ID
	
	// if($pid==0 || $aid==0) exit('{"status":"fail","msg":"参数错误"}');
	$today = date('Y-m-d');


	// 1.获取分销商
	$sql = "select r.id,r.son_id,m.dname,m.account from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.parent_id=$sid and r.ship_type=0 and r.status=0 and son_id>1 and son_id<>$sid limit 2";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$distributors[$row['son_id']] = array(
			'dname'   => $row['dname'],
			'account' => $row['account'],
		);
	}

	// 2.各个产品信息
	// $pids = array(10980,15642);
	// $pids = array(11555,11556);
	$pids = array(16506,16507,16508,16509,16510);
	// $aid = 6610;
	$aid = 0;
	$ltitle = '';
	$sql = "select t.title as ttitle,l.title as ltitle,p.id as pid from uu_products p left join uu_jq_ticket t on p.id=t.pid left join uu_land l on p.contact_id=l.id  where p.id in (".implode(',', $pids).") limit ".count($pids);
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$pInfos[$row['pid']] = $row;
		$ltitle = $row['ltitle'];
	}
	

	
	

	$prods = array();
	foreach($pInfos as $pid=>$pInfo){
		
		$dprice = $price = array();
		
		// 会员购买价格
		$price = abc777\Product::Price($inSide, $pid, $aid, $today, 0);
		$jsprice = $price['js'];
		
		// 3.获取分销商已配置的差价
		$dids_arr = array_keys($distributors);
		$dids_str = implode(',', $dids_arr);
		$dids_siz = count($dids_arr);
		$p_aid = $aid ? $sid:0;// 一级分销商
		$sql = "select dprice,pid from uu_priceset where tid=$pid and aid=$p_aid and pid in ($dids_str) order by id desc limit $dids_siz";
		$GLOBALS['le']->query($sql);
		while($row = $GLOBALS['le']->fetch_assoc()){
			$dprice[$row['pid']] = $row['dprice']/100;
		}
		
		// 4.整合数据，各分销商的分销价
		$prod = array('ttitle'=>$pInfo['ttitle'],'jsprice'=>$jsprice);
		foreach($distributors as $did=>$row){
			$row['js'] = isset($dprice[$did]) ? $jsprice+$dprice[$did]:'';
			// $prod['list'][] = $row;
			$prods[$did]['prods'][] = $prod;
		}
		// $prods[$pid] = $prod;
	}

	foreach($distributors as $did=>$dist){
		$distributors[$did]['list'] = $prods[$did]['prods'];
	}


	$response = array(
		'ltitle' => $ltitle,
		'dtotal' => count($distributors),
		'ptotal' => count($pids),
		'dlists'  => $distributors,
	);
	// print_r('<pre/>');
	// print_r($response);
	// print_r($response);
	echo json_encode($response);
	exit;
}

include_once("/var/www/html/new/d/common/header.html");
include_once("/var/www/html/new/d/common/left.html");
//include_once("/var/www/html/new/d/tpl/Price_allocation.tpl");
include_once("/var/www/html/new/d/tpl/set_price.tpl");
include_once("/var/www/html/new/d/common/footer.html");

?>